﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Fuse8.DomainFramework.Common;

using Fuse8.ContentManagementFramework.Web.MvcPowered;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.BizFacade.Services;

namespace Fuse8.ContentManagementFramework.Administration.Controls
{
    public static class CmsPageLocationDropDownListExtensions
    {
        public static CmsPageLocationDropDownList CmsAdminPageLocation(this HtmlHelper htmlHelper, CmsPage page, object htmlAttributes = null)
        {
            return new CmsPageLocationDropDownList(htmlHelper, page);
        }
    }

    public static class CmsPageContainerExtensions
    {
        public static CmsPageContainer FindElement(this IEnumerable<CmsPageContainer> source, CmsPage page)
        {
            Guard.ArgumentNotNull(source, "source");
            Guard.ArgumentNotNull(page, "page");

            return source.FirstOrDefault(p => p.Page.Equals(page));
        }
    }
    
    public class CmsPageContainer
    {
        public int Order { get; set; }

        public CmsPage Page { get; set; }

        public static bool operator >(CmsPageContainer first, CmsPageContainer second)
        {
            Guard.ArgumentNotNull(first, "first");
            Guard.ArgumentNotNull(second, "second");

            return first.Order > second.Order;
        }

        public static bool operator <(CmsPageContainer first, CmsPageContainer second)
        {
            return !(first > second);
        }
    }

    public class CmsPageLocationDropDownList : CmsMvcBaseComponent<CmsPageLocationDropDownList, IEnumerable<CmsPageContainer>>
    {
        private CmsPageService _pageService = new CmsPageService();

        protected virtual Func<IEnumerable<CmsPageContainer>, object> NoLocationTemplate { get; set; }

        public CmsPageLocationDropDownList(HtmlHelper htmlHelper, CmsPage page)
            : base(htmlHelper)
        {
            Guard.ArgumentNotNull(page, "page");

            var data = page.ParentPageId.HasValue ?
                    _pageService.GetChildPages(page.ParentPageId.Value, false, 1) :
                    new List<CmsPage>();

            int index = 0;

            Value = data.Select(
                p => new CmsPageContainer
                {
                    Page = p,
                    Order = index++
                }).ToList();

            Component = this;
        }

        public CmsPageLocationDropDownList NoElementsTemplate(Func<IEnumerable<CmsPageContainer>, object> template)
        {
            Guard.ArgumentNotNull(template, "template");

            NoLocationTemplate = template;

            return this;
        }

        protected override string RenderTemplate()
        {
            return Value.Any() ?
                base.RenderTemplate() :
                NoLocationTemplate != null ?
                    NoLocationTemplate(Value).ToString() :
                    string.Empty;
        }
    }
}